import { Controller,Post,Body,Request,Get,Param,Delete } from '@nestjs/common';
import { ApiBasicAuth, ApiCreatedResponse, ApiTags, ApiOperation, ApiProperty  } from '@nestjs/swagger'
import { InjectModel } from '@nestjs/mongoose'
import { IsNotEmpty } from 'class-validator'

class userSssionID {
  @ApiProperty({ description:'',example:'id(required)' })
  @IsNotEmpty({message:'id必填'})
  id:string
}

class userSssionDate {
  @ApiProperty({ description:'',example:'id(required)' })
  id:string
  @ApiProperty({ description:'',example:'用户名(required)'})
  @IsNotEmpty({message:'用户名必填'})
  username:string;
  @ApiProperty({ description:'',example:'身份标识(required) 0 游客 1 管理员'})
  @IsNotEmpty({message:'身份标识必填'})
  userSession: string
}

class userResponses {
  @ApiProperty({ description:'',example:'id' })
  id:string
  @ApiProperty({ description:'',example:'用户名'})
  @IsNotEmpty({message:'用户名必填'})
  username:string;
  @ApiProperty({ description:'',example:'身份标识 0 游客 1 管理员'})
  @IsNotEmpty({message:'身份标识必填'})
  userSession: string
  @ApiProperty({ description:'',example:'操作人'})
  anyname: string
  @ApiProperty({ description:'',example:'创建时间'})
  startTime:number
  @ApiProperty({ description:'',example:'修改时间'})
  lastTime:number
}
class userSssionQuery{
  @ApiProperty({ description:'',example:'搜索' })
  query: string
  @ApiProperty({ description:'',example:'页数' })
  pagenum:number
  @ApiProperty({ description:'',example:'条数' })
  pagesize:number
}

@ApiTags('信息修改')
@Controller('userssion')
export class UserssionController {
  constructor(
    @InjectModel('Cat') private readonly studentsModel,
) {}
@Post('anyUserSssion')
// 接口文档响应示例
@ApiCreatedResponse({description: '', type: userResponses})
@ApiOperation({ summary: '获取数据列表' })
  async anyShop(@Body() body:userSssionQuery){
    // 如果不传查询条件则默认请求10条数据
    body.pagenum = body.pagenum ? body.pagenum : 1
    body.pagesize = body.pagesize ? body.pagesize : 10
    var dataList = await this.studentsModel.find().sort({_id: -1})
    // 接口支持模糊搜索
    if (body.query!=='') {      
      dataList = dataList.filter(x=>{
       if(x.username.indexOf(body.query)!==-1){
         return x
       }
     })  
   }    
    // 剔除敏感信息
    dataList.forEach(x => {
      if (Array.prototype.slice.call(x, 'username'||'tokens')) {
         x.password = undefined;
         x.tokens = undefined;      
      }
    })
    // 分页
    var dataListNumber = body.pagenum*body.pagesize > dataList.length ? dataList.length : body.pagesize
    const dataLists=dataList.slice((body.pagenum-1)*body.pagesize, dataListNumber )

    return {status:'200',data:dataLists,total:dataList.length,message:'查询成功'}
  }

  // 获取详情
@Get(':id')
@ApiOperation({ summary: '获取详情' })
async userSssion(@Param() param:userSssionID){
  const lists = await this.studentsModel.findById({_id:param.id})
  return {status:'200',data:lists,message:'查询成功'}
}

// 更新接口
@Post('userssionSaves')
@ApiOperation({ summary: '更新接口' })
async userssionSaves(@Body() body:userSssionDate,@Request() request){
  const result = await this.studentsModel.find({tokens:request.headers.authorization})
  if (result[0].userSession==='0') return {status:'501',data:[],message:'权限等级不足'}
  const dataList = await this.studentsModel.findById({_id:body.id})
  dataList.anyname = result[0].username
  dataList.username = body.username
  dataList.userSession = body.userSession
  dataList.lastTime = Date.now()
  await dataList.save()
  return {status:'200',data:[],message:'更新成功'}
  }

// 删除接口
@Delete(':id')
@ApiOperation({ summary: '删除接口' })
async userssionRemove(@Param() param:userSssionID,@Request() request){
  const result = await this.studentsModel.find({tokens:request.headers.authorization})
  if (result[0].userSession==='0') return {status:'501',data:[],message:'权限等级不足'}
  const dataList = await this.studentsModel.findById({_id:param.id})
  await dataList.remove()
  // 返回删除信息的token值，让前端去清除登录状态
  return {status:'200',data:[],token:result[0].tokens,message:'更新成功'}
  }
}